#import "sprite_game_enemy_boss1_tail.h"

@implementation SpriteGameEnemyBoss1Tail

@synthesize tailY = tailY_;
@synthesize fixture = fixture_;
@synthesize flagActive = flagActive_;
@synthesize flagInvincible = flagInvincible_;

- (id)initSpriteGameEnemyTail:(bool)IsLeft
{
	NSString *_Path;

	if ((self = [super init]))
	{
		flagIsLeft_ = IsLeft;

		[self schedule:@selector(tick:)];

		[self CreateBody];

		_Path = [NSString stringWithUTF8String:g_CfgGameBoss1Data.tail_image_path];
		image_ = [CCSprite spriteWithSpriteFrameName:_Path];
		[self addChild:image_];
		image_.anchorPoint = ccp(1.0f, 0.5f);
		if (!IsLeft)
		{
			image_.scaleX = -1;
		}

		lifePoint_ = 0;
		flagActive_ = false;
		flagInvincible_ = false;
		flagRecover_ = false;

		maxLength_ = g_ScrrenSize.width * 0.75f;
	}
	return self;
}

- (void)dealloc
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
		fixture_ = NULL;
	}
	[super dealloc];
}

- (void)tick:(ccTime)Time
{
	float _EndX;

	if (NULL != box2dBody_)
	{
		box2dBody_->SetTransform([self ComputeBodyPosition], 0);
	}

	if (flagActive_ && flagRecover_)
	{
		_EndX = self.positionInPixels.x;
		if (flagIsLeft_)
		{
			if (_EndX < maxLength_)
			{
				_EndX += 1.0f;
				if (_EndX > maxLength_)
				{
					_EndX = maxLength_;
				}
			}
			self.positionInPixels = ccp(_EndX, tailY_);
		}
		else
		{
			if (_EndX > g_ScrrenSize.width - maxLength_)
			{
				_EndX -= 1.0f;
				if (_EndX < g_ScrrenSize.width - maxLength_)
				{
					_EndX = g_ScrrenSize.width - maxLength_;
				}
			}
			self.positionInPixels = ccp(_EndX, tailY_);
		}
	}
}

- (void)CreateBody
{
	int i;
	int _VertexCount;
	float _Ratio;
	b2Vec2 *_Vertex;
	b2BodyDef _BodyDef;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;

	_BodyDef.type = b2_dynamicBody;
	box2dBody_ = CMgrGamePhysics::World()->CreateBody(&_BodyDef);
	_Ratio = 1 / CMgrGamePhysics::Ratio();
	if (flagIsLeft_)
	{
		_VertexCount = g_CfgGameBoss1Data.box2d_collision_point_table_tail_left.size();
		_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss1Data.box2d_collision_point_table_tail_left);
	}
	else
	{
		_VertexCount = g_CfgGameBoss1Data.box2d_collision_point_table_tail_right.size();
		_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss1Data.box2d_collision_point_table_tail_right);
	}
	for (i = 0; i < _VertexCount; ++i)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	free(_Vertex);
	_PolygonShape.Set(_Vertex, _VertexCount);
	_FixtureDef.shape = &_PolygonShape;
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;
	fixture_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)DestroyBody
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
		fixture_ = NULL;
	}
}

- (b2Vec2)ComputeBodyPosition
{
	return ptConvertCGPoint(ptScalePoint_same(ccpAdd(self.positionInPixels, self.parent.positionInPixels), 1 / CMgrGamePhysics::Ratio()));
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[image_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[image_ resumeSchedulerAndActions];
}

- (void)Damage:(float)DamagePoint
{
	float _EndX;
	CCSequence *_Seq;
	id _ActionMoveBack;
	CCCallFuncN *_CallBackMoveBackOver;

	lifePoint_ -= DamagePoint;
	if (lifePoint_ <= 0)
	{
		lifePoint_ = 0;
		flagActive_ = false;
		flagInvincible_ = false;
		flagRecover_ = false;
		[self stopAllActions];
		[self ActionDisappear];
	}
	else
	{
		if (flagIsLeft_)
		{
			_EndX = self.positionInPixels.x - DamagePoint;
			if (_EndX < 10)
			{
				_EndX = 10;
			}
		}
		else
		{
			_EndX = self.positionInPixels.x + DamagePoint;
			if (_EndX > g_ScrrenSize.width - 10)
			{
				_EndX = g_ScrrenSize.width - 10;
			}
		}
		[self stopAllActions];
		_ActionMoveBack = tcCreateActionMove(self.positionInPixels, ccp(_EndX, tailY_), 100);
		_CallBackMoveBackOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionDamageBackOver:)];
		_Seq = [CCSequence actions:_ActionMoveBack, _CallBackMoveBackOver, nil];
		[self runAction:_Seq];
		flagRecover_ = false;
	}
}

- (void)ActionAppear
{
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	if (flagIsLeft_)
	{
		_Action = [action_move_x_in_pixel actionWithDuration:1.0f position:ccp(maxLength_, tailY_)];
	}
	else
	{
		_Action = [action_move_x_in_pixel actionWithDuration:1.0f position:ccp(g_ScrrenSize.width - maxLength_, tailY_)];
	}
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionAppearOver:)];
	_Seq = [CCSequence actions:_Action, _CallBack, nil];
	[self runAction:_Seq];
	lifePoint_ = g_CfgGameBoss1Data.tail_life_point;
	flagActive_ = true;
	flagInvincible_ = true;
	flagRecover_ = false;
}

- (void)ActionDisappear
{
	id _Action;

	if (flagIsLeft_)
	{
		_Action = [action_move_x_in_pixel actionWithDuration:1.0f position:ccp(0, tailY_)];
	}
	else
	{
		_Action = [action_move_x_in_pixel actionWithDuration:1.0f position:ccp(g_ScrrenSize.width, tailY_)];
	}
	[self runAction:_Action];
}

- (void)CallBackActionAppearOver:(id)Sender
{
	flagInvincible_ = false;
	flagRecover_ = true;
}

- (void)CallBackActionDamageBackOver:(id)Sender
{
	flagRecover_ = true;
}

@end